package com.yangxk.kadmin.web.module.gen.types;

import com.yangxk.kadmin.common.exception.BizException;
import com.yangxk.kadmin.generator.util.Tools;
import com.yangxk.kadmin.web.common.enumeration.ResponseCode;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

import java.math.BigDecimal;
import java.sql.Types;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class JavaTypeResolver {
    private Map<Integer, JdbcTypeInformation> typeMap;

    public JavaTypeResolver() {
        typeMap = new HashMap<>();
        typeMap.put(Types.CHAR, new JdbcTypeInformation("CHAR", String.class.getName()));
        typeMap.put(Types.VARCHAR, new JdbcTypeInformation("VARCHAR", String.class.getName()));
        typeMap.put(Types.CLOB, new JdbcTypeInformation("CLOB", String.class.getName()));
        typeMap.put(Types.BIT, new JdbcTypeInformation("BIT", Boolean.class.getName()));
        typeMap.put(Types.BOOLEAN, new JdbcTypeInformation("BOOLEAN", Boolean.class.getName()));
        typeMap.put(Types.DATE, new JdbcTypeInformation("DATE", Date.class.getName()));
        typeMap.put(Types.TIME, new JdbcTypeInformation("TIME", Date.class.getName()));
        typeMap.put(Types.TIMESTAMP, new JdbcTypeInformation("TIMESTAMP", Date.class.getName()));
        typeMap.put(Types.INTEGER, new JdbcTypeInformation("INTEGER", Integer.class.getName()));
        typeMap.put(Types.BIGINT, new JdbcTypeInformation("BIGINT", Long.class.getName()));
        typeMap.put(Types.FLOAT, new JdbcTypeInformation("FLOAT", Float.class.getName()));
        typeMap.put(Types.REAL, new JdbcTypeInformation("REAL", Float.class.getName()));
        typeMap.put(Types.DOUBLE, new JdbcTypeInformation("DOUBLE", Double.class.getName()));
        typeMap.put(Types.NUMERIC, new JdbcTypeInformation("NUMERIC", BigDecimal.class.getName()));
        typeMap.put(Types.DECIMAL, new JdbcTypeInformation("DECIMAL", BigDecimal.class.getName()));

    }

    private JdbcTypeInformation getJdbcTypeInformation(int types) {
        JdbcTypeInformation jdbcTypeInformation = typeMap.get(types);
        if (jdbcTypeInformation == null)
            throw new BizException(ResponseCode.NOT_SUPPORT_COLUMN_TYPE);
        return jdbcTypeInformation;
    }

    public String getJdbcTypeName(int types) {
        return getJdbcTypeInformation(types).getJdbcTypeName();
    }

    public String getJavaTypeName(int types) {
        return getJdbcTypeInformation(types).getJavaTypeName();
    }

    public String getJavaTypeShortName(int types) {
        return getJdbcTypeInformation(types).getJavaTypeShortName();
    }


    @Getter
    @Setter
    @AllArgsConstructor
    private static class JdbcTypeInformation {
        private String jdbcTypeName;
        private String javaTypeName;
        private String javaTypeShortName;// 短类型

        public JdbcTypeInformation(String jdbcTypeName, String javaTypeName) {
            this.jdbcTypeName = jdbcTypeName;
            this.javaTypeName = javaTypeName;
            this.javaTypeShortName = Tools.getClassName(javaTypeName);
        }
    }
}
